<?php

namespace app\services;

use plugin\admin\app\model\Language;
use plugin\admin\app\model\LanguagePosition;
use support\Cache;

class LanguageService
{
    public static function getLanguageData($language)
    {
        // Validate the language type
        if (!in_array($language, ['zh', 'en'])) {
            throw new \InvalidArgumentException('Invalid language type');
        }

        // Define cache key
        $cacheKey = "language_data_{$language}";

        // Check if data is in cache
//        $data = Cache::get($cacheKey);
//        if ($data) {
//            return $data;
//        }

        // Fetch data from database
        $positions = LanguagePosition::all();
        $languages = Language::where('language', $language)->get();

        // Structure the data
        $result = [];
        foreach ($positions as $position) {
            $positionKey = $position->position_key;
            $result[$positionKey] = [];

            foreach ($languages as $lang) {
                if ($lang->position_id == $position->id) {

                    $value = self::getProcessor("{$positionKey}.{$lang->key}")($lang->value);

                    $result[$positionKey][$lang->key] = $value;
                }
            }
        }

        // Store the data in cache
        Cache::set($cacheKey, $result, 3600); // Cache for 1 hour

        return $result;
    }

    public static function getSiteData($language)
    {
        $configs = ConfigService::getByGroup('site');

        if (get_request_type() == 'h5') {
            $copyright = explode('|', $configs['site_copyright'] ?? '');
            $enCopyright = explode('|', $configs['en_site_copyright'] ?? '');
        } else {
            $copyright = $configs['site_copyright'] ?? '';
            $enCopyright = $configs['en_site_copyright'] ?? '';
        }

        if ($language == 'zh') {
            return [
                'site' => [
                    'title' => $configs['site_name'] ?? '',
                    'description' => $configs['site_desc'] ?? '',
                    'keywords' => $configs['site_keywords'] ?? '',
                    'copyright' => $copyright,
                    'support' => $configs['site_tech_support'] ?? '',
                    'company_address' => $configs['company_address'] ?? '',
                    'contact_mobile' => $configs['contact_mobile'] ?? '',
                    'contact_qrcode' => full_url($configs['contact_qrcode']) ?? '',
                    'wechat_customer_service' => full_url($configs['wechat_customer_service']) ?? '',
                    'work_time' => $configs['work_time'] ?? '',
                ]
            ];
        } else {
            return [
                'site' => [
                    'title' => $configs['en_site_title'] ?? '',
                    'description' => $configs['en_site_description'] ?? '',
                    'keywords' => $configs['en_site_keywords'] ?? '',
                    'copyright' => $enCopyright,
                    'support' => $configs['en_site_tech_support'] ?? '',
                    'company_address' => $configs['en_company_address'] ?? '',
                    'contact_mobile' => $configs['contact_mobile'] ?? '',
                    'contact_qrcode' => full_url($configs['contact_qrcode']) ?? '',
                    'wechat_customer_service' => full_url($configs['wechat_customer_service']) ?? '',
                    'work_time' => $configs['work_time'] ?? '',
                ]
            ];
        }
    }

    public static function getProcessor($key)
    {

        $processor1 = function($value) {
            return explode('，', $value);
        };

        $processor = [
            'h5' => [
                'index.banner_big_title' => function($value) {
                    if (get_current_language() == 'zh') {
                        return [mb_substr($value, 0, 4), mb_substr($value, 4)];
                    } else {
                        return $value;
                    }
                },
                'index.material_big_title' => $processor1,
                'index.come_to_company' => $processor1,
                'footer.contact_big_title' => $processor1,
                'about.about_banner_title' => function($value) {
                    return explode("  ", $value);
                },
                'about.about_banner_desc' => function($value) {
                    if (get_current_language() == 'zh') {
                        return [mb_substr($value, 0, 13), mb_substr($value, 14)];
                    } else {
                        return $value;
                    }

                },
                'about.tech_3d_title' => function($value) {
                    if (get_current_language() == 'zh') {
                        return [mb_substr($value, 0, 6), mb_substr($value, 7)];
                    } else {
                        return $value;
                    }
                },
                'about.produce_device_desc' => function($value) {
                    $tempArr = explode('\n', $value);

                    $html = '';
                    if (count($tempArr) > 1) {
                        foreach ($tempArr as $value1) {
                            $html .= "<p>{$value1}</p>";
                        }
                    } else {
                        $html = $value;
                    }

                    return $html;
                },
                'vip.vip_desc' => function($value) {
                    return [mb_substr($value, 0, 19), mb_substr($value, 20)];
                },
                'index.design_big_title' => function($value) {
                    return [mb_substr($value, 0, 6), mb_substr($value, 6)];
                },
            ]
        ];

        $requestType = get_request_type();
        if ($requestType == 'h5') {
            return $processor['h5'][$key] ?? function($value) {
                $tempArr = explode("\n", $value);

                $html = '';
                if (count($tempArr) > 1) {
                    foreach ($tempArr as $value1) {
                        $html .= "<p>{$value1}</p>";
                    }
                } else {
                    $html = $value;
                }

                return $html;
            };
        } else {
            return function($value) {
                $tempArr = explode('\n', $value);

                $html = '';
                if (count($tempArr) > 1) {
                    foreach ($tempArr as $value1) {
                        $html .= "<p>{$value1}</p>";
                    }
                } else {
                    $html = $value;
                }

                return $html;
            };
        }
    }
}